package com.gun.management.module.raffle.controller.admin.lotteryruleref;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.Valid;
import jakarta.servlet.http.HttpServletResponse;
import java.util.*;
import java.io.IOException;

import com.gun.management.framework.common.pojo.PageParam;
import com.gun.management.framework.common.pojo.PageResult;
import com.gun.management.framework.common.pojo.CommonResult;
import com.gun.management.framework.common.util.object.BeanUtils;
import static com.gun.management.framework.common.pojo.CommonResult.success;

import com.gun.management.framework.excel.core.util.ExcelUtils;

import com.gun.management.module.raffle.controller.admin.lotteryruleref.vo.*;
import com.gun.management.module.raffle.dal.dataobject.lotteryruleref.LotteryRuleRefDO;
import com.gun.management.module.raffle.service.lotteryruleref.LotteryRuleRefService;

@Tag(name = "管理后台 - 抽奖-规则信息")
@RestController
@RequestMapping("/raffle/lottery-rule-ref")
@Validated
public class LotteryRuleRefController {

    @Resource
    private LotteryRuleRefService lotteryRuleRefService;

    @PostMapping("/create")
    @Operation(summary = "创建抽奖-规则信息")
    @PreAuthorize("@ss.hasPermission('raffle:lottery-rule-ref:create')")
    public CommonResult<Long> createLotteryRuleRef(@Valid @RequestBody LotteryRuleRefSaveReqVO createReqVO) {
        return success(lotteryRuleRefService.createLotteryRuleRef(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新抽奖-规则信息")
    @PreAuthorize("@ss.hasPermission('raffle:lottery-rule-ref:update')")
    public CommonResult<Boolean> updateLotteryRuleRef(@Valid @RequestBody LotteryRuleRefSaveReqVO updateReqVO) {
        lotteryRuleRefService.updateLotteryRuleRef(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除抽奖-规则信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('raffle:lottery-rule-ref:delete')")
    public CommonResult<Boolean> deleteLotteryRuleRef(@RequestParam("id") Long id) {
        lotteryRuleRefService.deleteLotteryRuleRef(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得抽奖-规则信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('raffle:lottery-rule-ref:query')")
    public CommonResult<LotteryRuleRefRespVO> getLotteryRuleRef(@RequestParam("id") Long id) {
        LotteryRuleRefDO lotteryRuleRef = lotteryRuleRefService.getLotteryRuleRef(id);
        return success(BeanUtils.toBean(lotteryRuleRef, LotteryRuleRefRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得抽奖-规则信息分页")
    @PreAuthorize("@ss.hasPermission('raffle:lottery-rule-ref:query')")
    public CommonResult<PageResult<LotteryRuleRefRespVO>> getLotteryRuleRefPage(@Valid LotteryRuleRefPageReqVO pageReqVO) {
        PageResult<LotteryRuleRefDO> pageResult = lotteryRuleRefService.getLotteryRuleRefPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LotteryRuleRefRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出抽奖-规则信息 Excel")
    @PreAuthorize("@ss.hasPermission('raffle:lottery-rule-ref:export')")
    public void exportLotteryRuleRefExcel(@Valid LotteryRuleRefPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LotteryRuleRefDO> list = lotteryRuleRefService.getLotteryRuleRefPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "抽奖-规则信息.xls", "数据", LotteryRuleRefRespVO.class,
                        BeanUtils.toBean(list, LotteryRuleRefRespVO.class));
    }

}